var express = require('express');
var router = express.Router();
var cinemasService = require('../service/cinemasService')

router.get('/all', async function (req, res) {
    let data = await cinemasService.findAll();
    console.log(data);
    res.send({
        data
    });
})

//添加院线
router.post('/', async function (req, res, next) {
    // [[1,0,1,1,1],[0,1,1,1,1]]
    let { name, addr, tel, url, screens } = req.body;
    screens = JSON.parse(screens);
    console.log("screens增加", screens);
    let data = await cinemasService.insert({ name, addr, tel, url, screens });
    // let data = await userService.insert({ username, pwd });
    res.send({ data });
});

//删除院线信息
router.delete('/:id', async function (req, res) {
    let { id } = req.params;
    let data = await cinemasService.remove({ _id: id });
    res.send({ data });
});

//修改院线信息
router.put('/:id', async function (req, res) {
    let { id } = req.params;
    let { name, addr, tel, url, screens } = req.body;
    screens = JSON.parse(screens);
    let data = await cinemasService.upData(id, { name, addr, tel, url, screens });
    res.send({ data });
});

//搜索
router.get('/', async function (req, res) {
    let { type, value, current, pageSize } = req.query;
    let searchObj = {};
    // console.log("type", type, "value", value);
    if (type) {
        if (type == "tel") {
            searchObj[type] = value;
        } else {
            searchObj[type] = { $regex: value };
        }
    }
    let data = await cinemasService.search(searchObj, { current: current || 1, pageSize: pageSize || 10 });
    res.send({ data });
});

// 通过id查找
router.get('/:id', async function (req, res) {
    let { id } = req.params;
    let data = await cinemasService.findById(id);
    res.send({ data });
});

module.exports = router;